#!/usr/bin/env python                         # -*- coding: utf-8 -*-

"""Convert files between the IQ formats and other formats."""

__author__ = 'Jouni K. Seppanen'
__version__ = '$Rev: 11 $'

from IQ.formats import *
import optparse

def main():
    p = optparse.OptionParser(usage="usage: %prog [options] inputfile outputfile",
			      version="%prog " + __version__,
			      description=__doc__)
    p.add_option("-i", "--inputformat", help="format of input file [autodetect]", 
		 metavar="FORMAT", default="autodetect",
		 type="choice", choices=["autodetect"]+formats.keys())
    p.add_option("-o", "--outputformat", help="format of output file [iq]",
		 metavar="FORMAT", default="iq",
		 type="choice", choices=formats.keys())
    p.add_option("-d", "--debug", help="enable debugging output",
		 action="store_true", dest="debug", default=False)
    opts, args = p.parse_args()
    if opts.debug:
	global debugging
	debugging = True
	debug("debugging output enabled")

    if len(args) != 2:
	p.error("must specify input and output files")

    if opts.inputformat == 'autodetect':
        infile = open(args[0], 'rb')
	opts.inputformat = autodetect(infile)
        infile.close()
	if opts.inputformat is None:
	    p.error("could not autodetect input file format")

    informat = formats[opts.inputformat]
    outformat = formats[opts.outputformat]

    convert(informat, args[0], outformat, args[1])

if __name__ == '__main__':
    main()
